package com.le.tester.tip;

import java.sql.Time;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

/**
 * createTime：2022/2/10 15:41
 * description：yield即当前线程让出当前cpu的时间片，那么这个线程就会从就绪的状态变成Runnable,然后和其他线程重新竞争资源
 */
public class YieldDemo1 implements Runnable {

    @Override
    public void run() {
        //做一个自增操作，并且输出相对应的值
        long resultValue = new AtomicLong(0L).incrementAndGet();
        System.out.println(resultValue);
        System.out.println(Thread.currentThread().getName() + " state is " + Thread.currentThread().getState());
        try {
            TimeUnit.SECONDS.sleep(10);
            System.out.println(Thread.currentThread().getName() + "结束了休眠");
        } catch (InterruptedException e) {
            System.out.println(e.getMessage());
        }
        Thread.yield();
        System.out.println(Thread.currentThread().getName() + " state is " + Thread.currentThread().getState());
        System.out.println("走到了这里");
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(new YieldDemo1(), "线程1");
        //等待cpu分配时间片
        t1.start();
        //这边需要进行signal的交互
        t1.interrupt();

//        Thread t2 = new Thread(new YieldDemo2(), "线程2");
//        t2.start();
    }
}
